﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;

namespace WinFormsApp1
{
    public partial class ParticleForm : Form
    {
        [DllImport("user32.dll")]
        static extern IntPtr GetDC(IntPtr hwnd);

        [DllImport("user32.dll")]
        static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);

        [DllImport("gdi32.dll")]
        static extern bool SetPixel(IntPtr hdc, int X, int Y, uint crColor);

        private IntPtr desktopDC;
        private List<Particle> particles = new List<Particle>();
        private Thread renderThread;
        private bool running = true;
        private Random rnd = new Random();

        public ParticleForm()
        {
            InitializeForm();
            InitializeParticles();
            desktopDC = GetDC(IntPtr.Zero);
            renderThread = new Thread(RenderLoop);
            renderThread.Start();
        }

        private void InitializeForm()
        {
            this.Text = "桌面粒子效果";
            this.Size = new Size(300, 100);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            this.FormClosing += ParticleForm_FormClosing;

            Button closeBtn = new Button { Text = "关闭", Dock = DockStyle.Fill };
            closeBtn.Click += (s, e) => this.Close();
            this.Controls.Add(closeBtn);
        }

        private void InitializeParticles()
        {
            for (int i = 0; i < 500; i++)
            {
                particles.Add(new Particle
                {
                    X = rnd.Next(Screen.PrimaryScreen.Bounds.Width),
                    Y = rnd.Next(Screen.PrimaryScreen.Bounds.Height),
                    VX = (rnd.NextDouble() - 0.5) * 4,
                    VY = (rnd.NextDouble() - 0.5) * 4,
                    Color = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256))
                });
            }
        }

        private void RenderLoop()
        {
            while (running)
            {
                UpdateParticles();
                DrawParticles();
                Thread.Sleep(30);
            }
        }

        private void UpdateParticles()
        {
            foreach (var p in particles)
            {
                p.X += p.VX;
                p.Y += p.VY;

                // 边界检查
                if (p.X < 0 || p.X >= Screen.PrimaryScreen.Bounds.Width) p.VX *= -1;
                if (p.Y < 0 || p.Y >= Screen.PrimaryScreen.Bounds.Height) p.VY *= -1;

                // 添加随机扰动
                p.VX += (rnd.NextDouble() - 0.5) * 0.2;
                p.VY += (rnd.NextDouble() - 0.5) * 0.2;

                // 速度限制
                if (Math.Abs(p.VX) > 5) p.VX *= 0.9;
                if (Math.Abs(p.VY) > 5) p.VY *= 0.9;
            }
        }

        private void DrawParticles()
        {
            foreach (var p in particles)
            {
                uint color = (uint)(p.Color.B << 16 | p.Color.G << 8 | p.Color.R);
                SetPixel(desktopDC, (int)p.X, (int)p.Y, color);
            }
        }

        private void ParticleForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            running = false;
            renderThread.Join(1000);
            ReleaseDC(IntPtr.Zero, desktopDC);
        }

        class Particle
        {
            public double X { get; set; }
            public double Y { get; set; }
            public double VX { get; set; }
            public double VY { get; set; }
            public Color Color { get; set; }
        }

        //[STAThread]
        //static void Main()
        //{
        //    Application.EnableVisualStyles();
        //    Application.SetCompatibleTextRenderingDefault(false);
        //    Application.Run(new ParticleForm());
        //}
    }
}